<?php

class Application_Model_DbTable_Articulo extends Zend_Db_Table_Abstract {

    protected $_name = 'articulos';

    /**
     * devuelve un arreglo con los datos del album con id=$id
     * @param  $id id del album
     * @return  arreblo asociativo
     */
    public function get($id) {
        $id = (int) $id;
        //$this->fetchRow devuelve fila donde id = $id
        try {
        	
       
	        $select = $this->_db->select();
	        $select->from(array("A" => 'articulos'), array('A.*', 'C.descripcion as descrip_categoria', 
	        'P.descripcion as descrip_proveedor', 'P50.descripcion as descrip_plan50', 'MARCA.descripcion as descrip_marca',
	        'GRUPO.descripcion as descrip_grupo', 'SUBGRUPO.descripcion as descrip_subgrupo', 'SUBCATEGORIA.descripcion as descrip_subcategoria' ));
	        $select->joinInner(array('C' => 'categorias'), 'A.categoria_id = C.id', array());
	        $select->joinInner(array('P' => 'proveedores'), 'A.proveedore_id = P.id', array());
	        $select->joinInner(array('P50' => 'plan50'), 'A.plan_50 = P50.id', array());
	        $select->joinInner(array('MARCA' => 'marcas'), 'A.marca = MARCA.id', array());
	        $select->joinInner(array('GRUPO' => 'grupos'), 'A.grupo = GRUPO.id', array());
	        $select->joinInner(array('SUBGRUPO' => 'subgrupos'), 'A.subgrupo = SUBGRUPO.id', array());
	        $select->joinInner(array('SUBCATEGORIA' => 'subcategorias'), 'A.subcategoria = SUBCATEGORIA.id', array());
	        $select->where('A.id=?',$id);
        	
        	
	        /*$logger = new Zend_Log();
			$writer = new Zend_Log_Writer_Stream(APPLICATION_LOGS_INV);
			$logger->addWriter($writer);	        
	        $logger->log('SQL articulo: ' .  $select->__toString(), Zend_Log::INFO);*/
		        
	        
	        
	        $result = $this->_db->fetchRow($select);
	
	        return $result;
	        
        } catch (Exception $e) {
        	throw new Exception($e);
        }
        
        
    }

   /*
     * Lista todos los Articulos
     */
    public function listarTodos() {
    	
        //devuelve todos los registros de la tabla
        $select = $this->select();
		$select->order('descripcion');         
        return $this->fetchAll($select);
    }
    
    public function imagenesPorArticulo($idArticulo) {
    	
        $select = $this->getAdapter()->select();
	    $select->from(array('A' => 'articulos'), array('AI.*', 'I.*'));
	    $select->joinInner(array('AI' => 'articulo_imagen'), 'A.id = AI.id_articulo', array());
	    $select->joinInner(array('I' => 'imagenes'), 'I.id = AI.id_imagen', array());
	    $select->where('A.id=?',$idArticulo);	        
        //$select = $this->select();        
        //return $this->fetchAll($select);
        //return $select;
        
        $result = $this->_db->fetchAll($select);
		return $result;
        
    }   
    
    public function listarPorProveedor($idProveedor) {
    	
    	$select = $this->select();
    	$select->where('proveedore_id=?',$idProveedor);
    	$select->order('descripcion');
    	return $this->fetchAll($select);
    	
    }

    public function listarPorPlan50($idPlan50) {
    	
    	$select = $this->select();
    	$select->where('plan_50=?',$idPlan50);
    	$select->order('descripcion');
    	return $this->fetchAll($select);
    	
    }

    public function listarPorCategoria($idCatgoria) {
    	
    	$select = $this->select();
    	$select->where('categoria_id=?',$idCatgoria);
    	$select->order('descripcion');
    	return $this->fetchAll($select);
    	
    }
    
    public function listarPorGrupo($idGrupo) {
    	
    	$select = $this->select();
    	$select->where('grupo=?',$idGrupo);
    	$select->order('descripcion');
    	return $this->fetchAll($select);
    	
    } 
    
    public function listarPorSubGrupo($idSubGrupo) {
    	
    	$select = $this->select();
    	$select->where('subgrupo=?',$idSubGrupo);
    	$select->order('descripcion');
    	return $this->fetchAll($select);
    	
    }     

    public function listarPorSubCategoria($idSubCategoria) {
    	
    	$select = $this->select();
    	$select->where('subcategoria=?',$idSubCategoria);
    	$select->order('descripcion');
    	return $this->fetchAll($select);
    	
    }
    
    function articulosAuditoria() {
    	
    	$select = $this->_db->select()->from(array('AA' => 'articulos_auditoria'), array('AA.idarticulo as id')) 
    	->joinInner(array('A' => 'articulos'), 'AA.idarticulo= A.id',
    	array('A.descripcion as descripcion'))
		->order('A.descripcion');
        return $this->_db->fetchAll($select);    	
    	
    }    

    function articulosNuevos() {
    	
    	$select = $this->_db->select()->from(array('AA' => 'articulos_nuevos'), array('AA.idarticulo as id')) 
    	->joinInner(array('A' => 'articulos'), 'AA.idarticulo= A.id',
    	array('A.descripcion as descripcion'))
		->order('A.descripcion');
        return $this->_db->fetchAll($select);    	
    	
    }     
    
    function articulosPromociones() {
    	
    	$select = $this->_db->select()->from(array('AA' => 'articulos_promociones'), array('AA.idarticulo as id')) 
    	->joinInner(array('A' => 'articulos'), 'AA.idarticulo= A.id',
    	array('A.descripcion as descripcion'))
		->order('A.descripcion');
        return $this->_db->fetchAll($select);    	
    	
    }    
    
}

